// pages/tweet/tweet.js
import {
  getPx
} from "../../../utils/util";
var tabHeight = 50;
Page({
  data: {
    tabIndex: 0,
    userId: '',
    isLogin: false,
    reload: false,
    newData: {
      type: 1,
      userId: ''
    },
    hotData: {
      type: 2,
      userId: ''
    },
    myData: {
      type: 3,
      userId: ''
    },
    tabnav: {
      tabitem: [{
          "id": 0,
          "text": "最新"
        },
        {
          "id": 1,
          "text": "热门"
        },
        {
          "id": 2,
          "text": "我的"
        },
      ]
    },
    winHeight: 0,
    height: 0,
    navBarHeight: 0,
    menuLeft: 0,
    menuBotton: 0,
    statusBarHeight: 0,
    buttonPosition: 0,
    showPublishButton: false,
  },

  onLoad: function (options) {
    var tabIndex = options.tabIndex || 0;
    tabHeight = getPx(tabHeight);
    var app = getApp();
    var id = app.globalData.userId;
    var height = app.globalData.windowHeight - tabHeight;
    var enableComment = app.globalData.versionCheckPassed;
    var that = this;
    if (height) {
      that.setData({
        tabIndex: tabIndex,
        userId: id,
        isLogin: app.globalData.isLogin,
        newData: {
          type: 1,
          userId: id,
          height: height,
          enableComment: enableComment,
        },
        hotData: {
          type: 2,
          userId: id,
          height: height,
          enableComment: enableComment,
        },
        myData: {
          type: 3,
          userId: id,
          height: height,
          enableComment: enableComment,
        },
        winHeight: height,
        statusBarHeight: app.globalData.statusBarHeight,
        navBarHeight: app.globalData.navBarHeight + app.globalData.menuBotton,
        menuLeft: app.globalData.menuLeft,
        menuBotton: app.globalData.menuBotton,
        buttonPosition: app.globalData.menuWidth + app.globalData.menuRight * 2,
        showPublishButton: app.globalData.versionCheckPassed && app.globalData.isLogin,
      });
    }
  },
  onShow: function () {
    var globalData = getApp().globalData;
    var reload = this.data.isLogin != globalData.isLogin;
    if(reload){
      var that = this;
      var id = globalData.userId;
      that.setData({
        userId: id,
        isLogin: globalData.isLogin,
        showPublishButton: globalData.versionCheckPassed && globalData.isLogin,
        myData: {
          type: 3,
          userId: id,
        },
      });
    }
  },

   /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (options) {
    var that = this;
    // 设置菜单中的转发按钮触发转发事件时的转发内容
    var tabIndex = that.data.tabIndex;
    var shareObj = {
      title: that.getShareTitle(tabIndex), // 默认是小程序的名称(可以写slogan等)
      path: '/pages/tweet/home/tweet?tabIndex=' + tabIndex, // 默认是当前页面，必须是以‘/’开头的完整路径
      // imageUrl: '', //自定义图片路径，可以是本地文件路径、代码包文件路径或者网络图片路径，支持PNG及JPG，不传入 imageUrl 则使用默认截图。显示图片长宽比是 5:4
      success: function (res) {
        // 转发成功之后的回调
        if (res.errMsg == 'shareAppMessage:ok') {}
      },
    };
    return shareObj;
  },
  onShareTimeline: function(){
    var that = this;
    var tabIndex = that.data.tabIndex;
    // 设置菜单中的转发按钮触发转发事件时的转发内容
    var shareObj = {
      title: that.getShareTitle(tabIndex), // 默认是小程序的名称(可以写slogan等)
      query: 'tabIndex=' + that.data.tabIndex, //当前页面路径携带的参数
      success: function (res) {
        // 转发成功之后的回调
        if (res.errMsg == 'shareAppMessage:ok') {}
      },
    };
    return shareObj;
  },

  getShareTitle: function(tabIndex){
    var userInfo = getApp().globalData.userInfo;
    var userName;
    if(userInfo){
      userName = userInfo.name || '有人';
    } else {
      userName = '有人';
    }
    return userName + '分享了' + this.data.tabnav.tabitem[tabIndex].text + '动弹';
  },

  /** 
   * 滑动切换tab 
   */
  bindChange: function (e) {
    var that = this;
    that.setData({
      tabIndex: e.detail.current
    });
  },
  /** 
   * 点击tab切换 
   */
  setTab: function (e) {
    const edata = e.currentTarget.dataset;
    this.setData({
      tabIndex: edata.tabindex,
    });
  },

  publishTweet: function () {
    var url;
    if(this.data.isLogin){
      url = '/pages/tweet/publish/publish';
    } else {
      url = '/pages/account/login/login';
    }
    wx.navigateTo({
      url: url,
    });
  }

})